<template>
  <div class="app-container">
    <page-tools>
      <template v-slot:after>
        <el-button
          type="primary"
          size="small"
          @click="updateType(1)"
        >添加权限</el-button>
      </template>
    </page-tools>
    <el-table border :data="list" style="width: 100%" row-key="id">
      <el-table-column prop="name" label="名称" width="width" />
      <el-table-column align="center" prop="code" label="标识" width="width" />
      <el-table-column
        align="center"
        prop="description"
        label="描述"
        width="width"
      />
      <el-table-column align="center" label="操作" width="width">
        <template v-slot="{ row }">
          <el-button
            v-if="row.type === 1"
            type="text"
            @click="updateType(2, row.id)"
          >增加</el-button>
          <el-button
            type="text"
            @click="getPermissionDetail(row.id)"
          >编辑</el-button>
          <el-button type="text" @click="delPermission(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="show" :title="title">
      <el-form ref="addPermissonForm" :model="formData" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-row :gutter="10" type="flex" align="center" justify="center">
          <el-col :span="6">
            <el-button-group>
              <el-button type="primary" @click="addPermission">确定</el-button>
              <el-button type="primary" @click="closeDialog">取消</el-button>
            </el-button-group>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  delPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/permisson'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      show: false,
      title: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const result = await getPermissionList()
      this.list = transListToTreeData(result, '0')
    },
    async addPermission() {
      if (!this.formData.id) {
        await addPermission(this.formData)
        this.$message.success('权限添加成功')
        this.closeDialog()
        this.getPermissionList()
      } else {
        await updatePermission(this.formData)
        this.$message.success('权限更新成功')
        this.closeDialog()
        this.getPermissionList()
      }
    },
    updateType(type, pid = '0') {
      this.title = '新增权限'
      this.show = true
      this.formData.type = type
      this.formData.pid = pid
    },
    delPermission(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delPermission(id)
          this.$message.success('权限删除成功')
          this.getPermissionList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async getPermissionDetail(id) {
      this.title = '编辑权限'
      this.show = true
      this.formData = await getPermissionDetail(id)
    },
    closeDialog() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.addPermissonForm.resetFields()
      this.show = false
    }
  }
}
</script>

<style></style>
